﻿using System;
using System.Collections.Generic;
using Missbot.Service;
using MongoDB.Bson.Serialization.Attributes;

namespace Missbot.AIModule.OnlineStore
{
    public class OSPersonBuff : DbBaseEntity
    {
        public string UserID { get; set; }
        public string Name { get; init; }
        public string Description { get; init; }
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime ExpiryTime { get; set; }
        public bool IsPositive { get; init; }
        public int Data { get; set; } = 1;
        public string Source { get; set; }
    }

    public static class OSPersonBuffDA
    {
        public static bool CheckBuff(string UserID, string Name)
        {
            return MongoService<OSPersonBuff>.GetOnly(p => p.UserID == UserID && p.Name == Name) != null;
        }

        public static List<OSPersonBuff> GetByIsPositive(string UserID, bool IsPositive)
        {
            return MongoService<OSPersonBuff>.Get(p => p.UserID == UserID && p.IsPositive == IsPositive);
        }

        public static List<OSPersonBuff> Get(string UserID)
        {
            return MongoService<OSPersonBuff>.Get(p => p.UserID == UserID);
        }

        public static void Add(this OSPersonBuff entity)
        {
            var buff = MongoService<OSPersonBuff>.GetOnly(p => p.UserID == entity.UserID && p.Name == entity.Name);
            if (buff == null)
            {
                MongoService<OSPersonBuff>.Insert(entity);
                return;
            }

            buff.ExpiryTime = entity.ExpiryTime;
            buff.Data = entity.Data;
            buff.Source = entity.Source;
            MongoService<OSPersonBuff>.Update(buff);
        }

        public static void Remove(this OSPersonBuff entity)
        {
            MongoService<OSPersonBuff>.Delete(entity);
        }

        public static void RemoveAll(string UserID)
        {
            MongoService<OSPersonBuff>.DeleteMany(p => p.UserID == UserID);
        }

        public static void Remove(string UserID, string BuffName)
        {
            MongoService<OSPersonBuff>.DeleteMany(p => p.UserID == UserID && p.Name == BuffName);
        }
    }
}